# This FOV method doesn't work yet.

import mod_direction
import mod_vector

def fieldOfView(actor):
    spaceQueue = []
    obsVectorMap = {}
    errorMap = {}
    visitedSpaces = set()
    
    start = actor.coords
    spaceQueue.append(start)
    
    while len(spaceQueue) > 0:
        space = spaceQueue.pop(0)
        if space in visitedSpaces:
            continue
        visitedSpaces.add(space)
        
        rightNeighbor = (space[0]+1,space[1])
        downNeighbor = (space[0],space[1]+1)
        
        propagate = (space in obsVectorMap)
        
        if not propagate:
            actor.markTileVisible(space)
            # Check if it's a wall...
            if actor.doesTileBlockVision(space):
                obsVector = mod_vector.subtractVectors(space,start)
                obsVectorMap[space] = obsVector
                errorMap[space] = obsVector
                propagate = True
                
        if propagate:
            obsVector = obsVectorMap[space]
            error = errorMap[space]
            
            # Propagate the data elsewhere:
            if error[0]>0:
                if rightNeighbor in obsVectorMap:
                    visitedSpaces.add(rightNeighbor)
                obsVectorMap[rightNeighbor] = obsVector
                # Our rightNeighbor's error is (ourError + (-y,y)), where (x,y) is the obsVector.
                errorChange = (-obsVector[1],obsVector[1])
                errorMap[rightNeighbor] = mod_vector.addVectors(error,errorChange)
                
            if error[1]>0:
                if downNeighbor in obsVectorMap:
                    visitedSpaces.add(downNeighbor)
                obsVectorMap[downNeighbor] = obsVector
                # Our downNeighbor's error is (ourError + (x,-x)), where (x,y) is the obsVector.
                errorChange = (obsVector[0],-obsVector[0])
                errorMap[downNeighbor] = mod_vector.addVectors(error,errorChange)
        
        if rightNeighbor in actor.level.spaceMap:
            spaceQueue.append(rightNeighbor)
        if downNeighbor in actor.level.spaceMap:
            spaceQueue.append(downNeighbor)
            